package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @RequiresPermissions(value={"permission:view","权限视图"},logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "permission/permission";
    }

    @RequiresPermissions(value={"permission:query","权限列表"},logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(QueryObject qo) {
        return permissionService.query(qo);
    }

    @RequiresPermissions(value={"permission:selectAll","权限全选"},logical = Logical.OR)
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Permission> selectAll() {
        return permissionService.selectAll();
    }

    @RequiresPermissions(value={"permission:load","权限加载"},logical = Logical.OR)
    @RequestMapping("load")
    @ResponseBody
    public JsonResult load() {
        try {
            permissionService.load();
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }
    @RequiresPermissions(value={"permission:getPermissionsByRoleId","回显权限"},logical = Logical.OR)
    @RequestMapping("getPermissionsByRoleId")
    @ResponseBody
    public List<Permission> getPermissionsByRoleId(Long roleId) {
        return permissionService.getPermissionsByRoleId(roleId);
    }
}
